<?php

namespace app\zc\controller;

use cmf\controller\AdminBaseController;
use app\zc\model\NavModel;
use app\zc\validate\NavValidate;
use think\Db;

class NavController extends AdminBaseController
{
    public function index()
    {
        $keyword = input('keyword');
        $where = [];
        if ($keyword) {
            $where[] = ['title', 'like', '%' . $keyword . '%'];
        }

        $NavList = NavModel::where($where)->order('list_order', 'desc')->paginate(10);

        $page = $NavList->render();

        $this->assign('NavList', $NavList);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $validate = new NavValidate();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }

            $Nav = new NavModel();
            $Nav->title = $data['title'];
            $Nav->url = $data['url'];

            if ($Nav->save()) {
                $this->success('添加成功', url('Nav/index'));
            } else {
                $this->error('添加失败');
            }
        }
    }

    public function toggleSwitch()
    {
        $id = $this->request->param('id', 0, 'intval');
        $news = NavModel::find($id);
        if ($news) {
            $news->switch = $news->switch ? 0 : 1;
            if ($news->save()) {
                return json(['status' => 1, 'msg' => '状态切换成功', 'switch' => $news->switch]);
            } else {
                return json(['status' => 0, 'msg' => '状态切换失败']);
            }
        } else {
            return json(['status' => 0, 'msg' => '新闻不存在']);
        }
    }

    public function edit($id)
    {
        $nav = NavModel::find($id);
        if (!$nav) {
            $this->error('导航不存在');
        }

        $this->assign('nav', $nav);
        return $this->fetch();
    }


    public function editPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            $validate = new NavValidate();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }

            $Nav = NavModel::find($data['id']);
            if (!$Nav) {
                $this->error('导航不存在');
            }

            $Nav->title = $data['title'];
            $Nav->url = $data['url'];
            $Nav->dept_id = $data['dept_id'];

            if ($Nav->save()) {
                $this->success('编辑成功', url('Nav/index'));
            } else {
                $this->error('编辑失败');
            }
        }
    }



    public function delete($id)
    {
        $Nav = NavModel::find($id);
        if (!$Nav) {
            $this->error('导航不存在');
        }

        if (NavModel::destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function order()
    {
        parent::listOrders(new NavModel());
        $this->success('排序成功');
    }

}
